
from sqlalchemy.orm import Session
from redis import Redis
from app import conf, ctx_conf

import logging
logger = logging.getLogger(__name__)



class CacheUtil():
    "缓存工具"

    r: Redis
    db: Session
    order_list_key: str = 'task:order_list'

    def __init__(self, db: Session):
        logger.debug('>> init CacheUtil')
        app = ctx_conf.get('app')
        self.r = app.r
        self.db = db

    def sadd(self, name, members: tuple):
        res = self.r.sadd(name, *members)
        logger.debug(f'added: {res}')

    def push_order_no(self, order_no):
        logger.debug(f'>> push_order_no: {order_no}')
        self.r.rpush(self.order_list_key, order_no)

    def pop_order_no(self):
        logger.debug('>> pop_order_no')
        return self.r.rpop(self.order_list_key)

        
